/* global window */
import modelExtend from 'dva-model-extend'
import * as dataDictionaryService from './services/dataDictionary'
import * as roleService from '../role/services/roles'
import * as empService from '../employee/services/employees'
import { pageModel } from 'utils/model'
import { arrayToTree } from "utils"
import { message } from 'antd'

const {
  getModals,
  getDictionarys,
  updateDictionaryItems,
  addDictionaryItems,
  deleteDictionaryItems,
  getBlacklists,
  getWhitelists,
  addWhiteList,
  deleteWhiteList,
  batchDeleteWhiteList,
  addBlackList,
  deleteBlackList,
  batchDeleteBlackList,
  updateBlackList,
  getSensitiveWords,
  addSensitiveWord,
  updateSensitiveWord,
  deleteSensitiveWord
} = dataDictionaryService

const initSelected = (datas, selectedRowKeys) => {
  if (datas && datas.length > 0) {
    for (let i = 0; i < datas.length; i++) {
      let item = datas[i]

      if (item.authorized) {
        selectedRowKeys.push(item.id)
      }
      if (item.children) {
        initSelected(item.children, selectedRowKeys)
      }
    }
  }
}

export default modelExtend(pageModel, {
  namespace: 'dataDictionary',

  state: {
    selectedRowKeys: [],
    expandedRowKeys: [],
    memberSelectedKeys: [],
    selectedRowOfBlackList: [],
    selectedBlackListRowKeys: [],
    selectedRowOfWhiteList: [],
    selectedWhiteListRowKeys: [],
    modalVisible: false,
    selectedRoleId: -1,
    memberModalVisible: false,
    modals: [],
    roles: [],
    buttons: [],
    data: {},
    dictionarys: [],
    blackList: [],
    whiteList: [],
    sensitiveWordsList: []
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/dataDictionary') {
          dispatch({
            type: "getDictionarys"
          })
          // dispatch({
          //   type: 'getBlacklists'
          // })
          // dispatch({
          //   type: 'getWhitelists'
          // })
          // dispatch({
          //   type: 'getSensitiveWords'
          // })
        }
      })
    },
  },

  effects: {
    *getDictionarys({ payload = {} }, { call, put }) {
      const response = yield call(dataDictionaryService.getDictionarys)
      if (response) {
        // debugger
        yield put({
          type: "updateState",
          payload: {
            dictionarys: response.data
          }
        })
      }
    },

    *updateDictionaryItems({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.updateDictionaryItems, payload)
      const { code, msg, data } = response
      if (code === 0) {
        message.success(msg)
        console.log(data)
        // yield put({
        //   type: "updateDictionary",
        //   payload: data
        // })

        //后端返回的parentId有问题，暂时更新完重新请求一次查询页接口
        yield put({
          type: "getDictionarys"
        })
      } else {
        message.error(msg)
      }
    },

    *addDictionaryItems({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.addDictionaryItems, payload)
      const { code, msg, data } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "updateDictionary",
          payload: data
        })
      } else {
        message.error(msg)
      }
    },

    *deleteDictionaryItems({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.deleteDictionaryItems, { id: payload.id })
      const { code, msg } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "removeDictionaryItems",
          payload
        })
      } else {
        message.error(msg)
      }
    },

    *getBlacklists({ payload = {} }, { call, put }) {
      const response = yield call(dataDictionaryService.getBlacklists, payload)
      const { data } = response
      if (response) {
        yield put({
          type: "updateState",
          payload: {
            blackList: data.records,
            blackListPage: {
              showSizeChanger: true,
              showQuickJumper: true,
              showTotal: total => `总共有 ${total} 条数据`,
              total: 0,
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          }
        })
      }
    },

    *addBlackList({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.addBlackList, payload)
      const { code, msg, data } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "getBlacklists"
        })
      } else {
        message.error(msg)
      }
    },

    *updateBlackList({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.updateBlackList, payload)
      const { code, msg } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "updateState",
          payload: {
            modalVisible: false
          }
        })
        yield put({
          type: "getBlacklists"
        })
      } else {
        message.error(msg)
      }
    },

    *deleteBlackList({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.deleteBlackList, payload)
      const { code, msg } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "updateState",
          payload: {
            selectedBlackListRowKeys: [],
            selectedRowOfBlackList: []
          }
        })
        yield put({
          type: "getBlacklists"
        })
      } else {
        message.error(msg)
      }
    },

    *batchDeleteBlackList({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.batchDeleteBlackList, payload)
      const { code, msg } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "updateState",
          payload: {
            selectedBlackListRowKeys: [],
            selectedRowOfBlackList: []
          }
        })
        yield put({
          type: "getBlacklists"
        })
      } else {
        message.error(msg)
      }
    },

    *getWhitelists({ payload = {} }, { call, put }) {
      const response = yield call(dataDictionaryService.getWhitelists, payload)
      const { data } = response
      if (response) {
        yield put({
          type: "updateState",
          payload: {
            whiteList: data.records,
            whiteListPage: {
              showSizeChanger: true,
              showQuickJumper: true,
              showTotal: total => `总共有 ${total} 条数据`,
              total: 0,
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.total,
            },
          }
        })
      }
    },

    *addWhiteList({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.addWhiteList, payload)
      const { code, msg, data } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "getWhitelists"
        })
      } else {
        message.error(msg)
      }
    },

    *deleteWhiteList({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.deleteWhiteList, payload)
      const { code, msg } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "updateState",
          payload: {
            selectedWhiteListRowKeys: [],
            selectedRowOfWhiteList: []
          }
        })
        yield put({
          type: "getWhitelists"
        })
      } else {
        message.error(msg)
      }
    },

    *batchDeleteWhiteList({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.batchDeleteWhiteList, payload)
      const { code, msg } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "updateState",
          payload: {
            selectedWhiteListRowKeys: [],
            selectedRowOfWhiteList: []
          }
        })
        yield put({
          type: "getWhitelists"
        })
      } else {
        message.error(msg)
      }
    },

    *getSensitiveWords({ payload = {} }, { call, put }) {
      const response = yield call(dataDictionaryService.getSensitiveWords)
      if (response) {
        // debugger
        yield put({
          type: "updateState",
          payload: {
            sensitiveWordsList: response.data
          }
        })
      }
    },

    *addSensitiveWord({ payload }, { call, put, select }) {
      const response = yield call(dataDictionaryService.addSensitiveWord, payload)
      const { code, msg, data } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "updateSensitiveWordsList",
          payload: data
        })
      } else {
        message.error(msg)
      }
    },

    *updateSensitiveWord({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.updateSensitiveWord, payload)
      const { code, msg, data } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "updateSensitiveWordsList",
          payload: data
        })
      } else {
        message.error(msg)
      }
    },

    *deleteSensitiveWord({ payload }, { call, put }) {
      const response = yield call(dataDictionaryService.deleteSensitiveWord, payload)
      const { code, msg } = response
      if (code === 0) {
        message.success(msg)
        yield put({
          type: "removeSensitiveWord",
          payload
        })
      } else {
        message.error(msg)
      }
    }

  },

  reducers: {
    updateDictionary(state, { payload }) {
      let newDictionarys = [...state.dictionarys]
      let index = newDictionarys.findIndex(item => item.id === payload.parentId)
      let items = newDictionarys[index].items
      let itemIndex = items.findIndex(item => item.id === payload.id)
      if (itemIndex === -1) {
        items.push(payload)
        return { ...state, dictionarys: newDictionarys }
      } else {
        items.splice(itemIndex, 1, payload)
        return { ...state, dictionarys: newDictionarys }
      }
    },

    removeDictionaryItems(state, { payload }) {
      let newDictionarys = [...state.dictionarys]
      let index = newDictionarys.findIndex(item => item.id === payload.parentId)
      let items = newDictionarys[index].items
      let itemIndex = items.findIndex(item => item.id === payload.id)
      items.splice(itemIndex, 1)
      return { ...state, dictionarys: newDictionarys }
    },

    updateSensitiveWordsList(state, { payload }) {
      let index = state.sensitiveWordsList.findIndex(item => item.id === payload.id)
      let newSensitiveWordsList = [...state.sensitiveWordsList]
      if (index === -1) {
        newSensitiveWordsList.push(payload)
        return { ...state, sensitiveWordsList: newSensitiveWordsList }
      } else {
        newSensitiveWordsList.splice(index, 1, payload)
        return { ...state, sensitiveWordsList: newSensitiveWordsList }
      }
    },

    removeSensitiveWord(state, { payload }) {
      let index = state.sensitiveWordsList.findIndex(item => item.id === payload.id)
      let newSensitiveWordsList = [...state.sensitiveWordsList]
      newSensitiveWordsList.splice(index, 1)
      return { ...state, sensitiveWordsList: newSensitiveWordsList }
    }
  },
})
